package com.spring.cloud.mybatisplus.config;

import com.baomidou.mybatisplus.entity.TableInfo;
import com.baomidou.mybatisplus.mapper.AutoSqlInjector;
import org.apache.ibatis.builder.MapperBuilderAssistant;
import org.apache.ibatis.mapping.SqlSource;
import org.apache.ibatis.session.Configuration;
import org.springframework.stereotype.Component;

@Component
//实现免mapper.xml文件 和 xxxMapper一样的调用方式
public class MySqlInjector extends AutoSqlInjector {

    @Override
    public void inject(Configuration configuration, MapperBuilderAssistant builderAssistant,
                       Class<?> mapperClass, Class<?> modelClass, TableInfo table) {
        /* 添加一个自定义方法 */
        deleteAll(mapperClass,modelClass,table);

    }

    public void deleteAll(Class<?> mapperClass, Class<?> modelClass, TableInfo table){
        /* 执行 SQL ，动态 SQL 参考类 SqlMethod */
        String sql = "delete from "+ table.getTableName();
        /* mapper 接口方法名一致 */
        String method = "deleteAll";
        SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass);
        this.addDeleteMappedStatement(mapperClass,method,sqlSource);
    }


}
